package od_2025A.one;

import java.util.*;

/**
 * https://blog.csdn.net/qq_45776114/article/details/146410589
 * 1、这道题看着是在考hash 和 贪心算法
 * 2、但是我并不懂贪心算法，我仔细去拆解这道题，应该也是可以搞定的
 */
public class Test02 {


    public static void main(String[] args) {
        // 0、获取已有的牌（手中的和已经发出的）
        Scanner scanner = new Scanner(System.in);
        String[] card1 = scanner.nextLine().split("-");
        String[] card2 = scanner.nextLine().split("-");
        List<String> existCard = new ArrayList<>(card1.length + card2.length);
        for (int i = 0; i < card1.length; i++) {
            existCard.add(card1[i]);
        }
        for (int i = 0; i < card2.length; i++) {
            existCard.add(card2[i]);
        }
        // 1、枚举所有的牌
        List<String> allCard = Arrays.asList("3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A");
        HashMap<String, Integer> cardValueMap = new HashMap<>(12);
        HashMap<String, Integer> cardCountMap = new HashMap<>(12);
        int value = 3;
        for (String card : allCard) {
            cardValueMap.put(card, value++);
            cardCountMap.put(card, 4);
        }
        // 2、算出剩余的牌，并且剩余牌有序
        for (String card : existCard) {
            if (cardCountMap.containsKey(card)){
                cardCountMap.put(card, cardCountMap.get(card) - 1);
            }
        }
        List<String> leftCard = new ArrayList<>();
        for (String s : allCard) {
            if (cardCountMap.get(s) != 0) {
                leftCard.add(s);
            }
        }
        if (leftCard.isEmpty()) {
            System.out.println("NO-CHAIN");
            return;
        }
        // 3、遍历剩余的牌，再将剩余的牌连成顺子
        List<List<String>> res = new ArrayList<>();
        List<String> first = new ArrayList<>();
        first.add(leftCard.get(0));
        res.add(first);

        for (int i = 1; i < leftCard.size(); i++) {
            boolean isMatch = false;
            String currentCard = leftCard.get(i);
            for (List<String> children : res) {
                Integer cardValue = cardValueMap.get(currentCard);
                Integer childEndValue = cardValueMap.get(children.get(children.size() - 1));
                if (cardValue - childEndValue == 1) {
                    children.add(currentCard);
                    isMatch = true;
                    break;
                }
            }
            if (!isMatch) {
                List<String> item = new ArrayList<>();
                item.add(currentCard);
                res.add(item);
            }
        }
        // 4、找出最大顺子
        List<String> maxList = res.get(0);
        for (List<String> re : res) {
            maxList = re.size() > maxList.size() ? re : maxList;
        }
        if (maxList.size() < 5) {
            System.out.println("NO-CHAIN");
            return;
        }
        System.out.println(String.join("-", maxList));
    }
}
